<?php
//
namespace app\home\controller;

use app\common\model\UserMemberModel;
use app\common\model\FavoriteModel;
use app\common\model\UserModel;
use think\Request;
use think\Session;

class PublicController extends BaseController
{

    private $userModel;
    private $memberModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel   = new UserModel();
        $this->memberModel = new UserMemberModel();
    }

    /*
     * 会员登录
     */
    public function userLogin()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
           //if (!captcha_check($data['reg-vcode'])) {
               // $this->error('验证码错误');
            //};
            $result = $this->validate($data, 'UserValidate.login');
            if (!$result) {
                $this->error($result);
            }
            $res = $this->userModel->checkLogin($data['username'], $data['password'], 'member');
            if (!$res) {
                $this->error($this->userModel->getError());
            }
            cookie('memberDes', str_encode($res),2592000);
            $data['url'] = empty($data['url']) ? url('home/Index/index') : $data['url'];
            $this->success('登录成功！', urldecode($data['url']));
        }
        $url=$this->request->param('url');
        $this->assign('backUrl',$url);
        return $this->fetch();
    }


    /*
    * 会员注册
    */
    public function userReg()
    {
        if ($this->request->isPost()) {
            $data    = $this->request->param();
            if (Session::has('sms')){
               $code=Session::get('sms');
               if ($code['time']+600<time()) {
                  $this->error('短信验证码过期');
               }elseif ($data['code']!==$code['code']){
                    $this->error('短信验证码错误');
                }
            }else{
               $this->error('请先获取短信验证码');
          }
           Session::delete('sms');
            $result = $this->validate($data, 'UserMemberValidate.reg');
            if (true!==$result) {
                $this->error($result);
           }
            $data += ['utype' => 'member', 'group_id' => 5, 'status' => 1];
            $res = $this->userModel->addData($data);
            if ($res !== false) {
                cookie('memberDes', str_encode($this->userModel->uid));
                $this->success('注册成功！', url('home/Index/index'));
            } else {
                $this->error($this->userModel->getError());
            }
        }
        return $this->fetch();
    }

    /*
    * 找回密码
    */
    public function findPwd()
    {
        return $this->fetch();
    }

    /**
     * 获取收藏数量和是否收藏过
     * @return mixed
     */
    public function getFavorite(){
        $uid         = $this->request->param('uid');
        $infoId         = $this->request->param('info_id/d');
        $channel        = $this->request->param('channel/d');
        $map['channel'] = $channel;
        $map['info_id'] = $infoId;
        $favoriteModel  = new FavoriteModel();
        $data['count']=$favoriteModel->where($map)->count();
        $map['uid']     = $uid;
        if ($favoriteModel->where($map)->find()) {
            $data['status']=1;
        }else{
            $data['status']=0;
        }
        return $data;
    }

    /**
     * 邮箱验证
     */
    public function verifyMail()
    {
        $parm=$this->request->get('parm','');
        $parm=json_decode(str_decode(urldecode($parm)),true);
        $model=new UserModel();
        if ($model->where('uid',$parm['uid'])->setField('email',$parm['email'])){
            $this->success('邮箱验证成功','/home/index/index');
        }else{
            $this->error('邮箱验证失败','/home/index/index');
        }
    }

    /**
     *微信验证
     */
    public function verifyWechat()
    {
        /* 创建接口操作对象 */
        $wechat = &load_wechat('Receive');

        /* 验证接口 */
        if ($wechat->valid() === FALSE) {
            // 接口验证错误，记录错误日志
            // log_message('ERROR', "微信被动接口验证失败，{$wechat->errMsg}[{$wechat->errCode}]");
            // 退出程序
            exit($wechat->errMsg);
        }
    }

    /**
     * 发送短信验证码
     */
    public function sendSms()
    {
        $to=$this->request->param('to');
        $vcode=$this->request->param('vcode');
        //if(!captcha_check($vcode)){
            //$this->error('验证码输入错误');
       // };
        $code=rand_string(4,1);

        Session::set('sms.code', $code);
        Session::set('sms.time',time());
        $this->success('暂时没有短信验证，输入'.$code.'即可');
        $data=[
            $code,
            '10分钟'
        ];
        $tempId='193921';
        $info=send_sms($to,$data,$tempId);
        if(!$info['code']){
            Session::set('sms.code', $code);
            Session::set('sms.time',time());
            $this->success('短信发送成功');
        }else{
            $this->error($info['msg']);
        }
    }
}
